/**********************************************************************************
// Level 2 Source File
// 
// Creation:	06 Jul 2011
// Updated:		05 Ago 2011
// Compiler:	Visual Studio 2010
//
// Notes:		Level 2 of Space Race
//
**********************************************************************************/

#include "SpaceRace.h"

/**********************************************************************************/

void SpaceRace::Level2Init()
{
	// reset space ship to its initial state
	int dx = -150;		// space ship delta from the world center 
	int dy = 350;		// space ship  delta from the world center 
	spaceShip->Reset();
	spaceShip->Animate(INERTIA);
	spaceShip->MoveTo(GetMidGameWorldX()+dx, GetMidGameWorldY()+dy);
	spaceShip->Rotate(-45);

	// register ship to scene manager
	scene.RegisterMoving(*spaceShip);

	/*****************************************/

	// reset the player viewport to its initial position
	playerVpBox.left = 0.0f + (GetGameWorldWidth() - GetWindowWidth())/2 + dx;
	playerVpBox.top = 0.0f + (GetGameWorldHeight() - GetWindowHeight())/2 + dy;
	playerVpBox.right = playerVpBox.left + (GetWindowWidth()-1);
	playerVpBox.bottom = playerVpBox.top + (GetWindowHeight()-1);

	// set initial viewport position
	viewport.left   = 0.0f + (GetGameWorldWidth() - GetWindowWidth())/2;
	viewport.top    = 160.0f + (GetGameWorldHeight() - GetWindowHeight())/2;
	viewport.right  = viewport.left + (GetWindowWidth()-1);
	viewport.bottom = viewport.top + (GetWindowHeight()-1);
	
	// update viewport
	if (viewport.left < 0)
	{
		viewport.left  = 0.0f;
		viewport.right = GetWindowWidth()-1.0f;
	}
	else if (viewport.right > GetGameWorldWidth()-1)
	{
		viewport.right = GetGameWorldWidth()-1.0f;
		viewport.left  = float(GetGameWorldWidth() - GetWindowWidth());
	}
	
	// ---------------------------------------------------
	
	if (viewport.top < 0)
	{
		viewport.top  = 0.0f;
		viewport.bottom = GetWindowHeight()-1.0f;
	}
	else if (viewport.bottom >  GetGameWorldHeight()-1)
	{
		viewport.bottom = GetGameWorldHeight()-1.0f;
		viewport.top    = float(GetGameWorldHeight() - GetWindowHeight());
	}

	/*****************************************/

	// initialize positions of interface components
	levelbar->MoveTo(viewport.left + GetMidWindowX(), viewport.top + 150.0f);
	helpbar->MoveTo(viewport.left + GetMidWindowX(), viewport.top + 350.0f);
	keymapbar->MoveTo(viewport.left + GetMidWindowX(), viewport.bottom - 15.0f);

	// create vertical bar sprite
	stationTwo = new Sprite("Resources/StationTwo.png");
	stationTwo->customType = objFixed;
	stationTwo->boundingbox = new Rect(-15.0f, -200.0f, 14.0f, 200.0f);
	stationTwo->boundingbox->color = Color(128,128,128);
	stationTwo->boundingbox->customType = objFixed;
	stationTwo->MoveTo(float(GetMidGameWorldX()),GetMidGameWorldY()+400.0f);
	fixed.push_back(stationTwo);
	scene.RegisterStatic(*stationTwo);

	// create space waste 1
	Sprite * waste11 = new Sprite("Resources/Waste11.png", 120, 120, 6);
	waste11->customType = objWaste;
	Point waste11vList[4] = {Point(29,8),Point(24,50),Point(-28,1),Point(-13,-52)};
	waste11->boundingbox = new Poly(waste11vList, 4); 
	waste11->boundingbox->customType = objWaste;
	waste11->boundingbox->color = Color(128,128,128);
	waste11->MoveTo(float(GetMidGameWorldX()-300),float(GetMidGameWorldY()+50));
	moving.push_back(waste11);
	scene.RegisterMoving(*waste11);

	// create space waste 2
	Sprite * waste12 = new Sprite("Resources/Waste12.png", 120, 120, 6);
	waste12->customType = objWaste;
	Point waste12vList[3] = {Point(42,-31),Point(42,36),Point(-47,-26)};
	waste12->boundingbox = new Poly(waste12vList, 3); 
	waste12->boundingbox->customType = objWaste;
	waste12->boundingbox->color = Color(128,128,128);
	waste12->MoveTo(float(GetMidGameWorldX()-300),float(GetMidGameWorldY()-250));
	moving.push_back(waste12);
	scene.RegisterMoving(*waste12);

	// create space waste 3
	Sprite * waste13 = new Sprite("Resources/Waste13.png", 120, 120, 6);
	waste13->customType = objWaste;
	Point waste13vList[4] = {Point(-52,7),Point(-14,-23),Point(51,-14),Point(51,23)};
	waste13->boundingbox = new Poly(waste13vList, 4); 
	waste13->boundingbox->customType = objWaste;
	waste13->boundingbox->color = Color(128,128,128);
	waste13->MoveTo(float(GetMidGameWorldX()),float(GetMidGameWorldY()-400));
	moving.push_back(waste13);
	scene.RegisterMoving(*waste13);

	// create space waste 4
	Sprite * waste14 = new Sprite("Resources/Waste14.png", 120, 120, 6);
	waste14->customType = objWaste;
	Point waste14vList[5] = {Point(-44,-25),Point(16,-25),Point(16,1),Point(48,1),Point(8,36)};
	waste14->boundingbox = new Poly(waste14vList, 5); 
	waste14->boundingbox->customType = objWaste;
	waste14->boundingbox->color = Color(128,128,128);
	waste14->MoveTo(float(GetMidGameWorldX()+300),float(GetMidGameWorldY()-250));
	moving.push_back(waste14);
	scene.RegisterMoving(*waste14);

	// create space waste 5
	Sprite * waste15 = new Sprite("Resources/Waste15.png", 120, 120, 6);
	waste15->customType = objWaste;
	Point waste15vList[5] = {Point(-32,-3),Point(-5,-3),Point(-5,-38),Point(46,27),Point(-43,19)};
	waste15->boundingbox = new Poly(waste15vList, 5); 
	waste15->boundingbox->customType = objWaste;
	waste15->boundingbox->color = Color(128,128,128);
	waste15->MoveTo(float(GetMidGameWorldX()+300),float(GetMidGameWorldY()+50));
	moving.push_back(waste15);
	scene.RegisterMoving(*waste15);

	// initialize finish marker
	finPosX = GetMidGameWorldX()-dx;
	finPosY = GetMidGameWorldY()+dy;
	finishBB = Rect(-42, -12, 41, 11);
	finishBB.MoveTo(finPosX,finPosY);
	finishBB.color = Color(128,128,128);
	finishBB.customType = objFinish;
	scene.RegisterStatic(finishBB);

	finish[0]->MoveTo(float(finPosX),float(finPosY));
	finish[1]->MoveTo(float(finPosX),float(finPosY));
	finish[2]->MoveTo(float(finPosX),float(finPosY));

	// level time limit
	timeLimit = 15;
	elapsedTime = 0;

	// number of dots traced
	ZeroMemory(&playerTrace, sizeof(playerTrace));
	dotsNum = 0;

	// initial finish color is green
	finishColor = Color(0,255,0);
	finishColorIndex = GREEN;
	viewAchievements = false;
	inspectResult = false;

	// initialize achievements
	achievements->pedal = true;
	ZeroMemory(achievements->redundantR, sizeof(achievements->redundantR));
	ZeroMemory(achievements->redundantL, sizeof(achievements->redundantL));
}

/**********************************************************************************/

void SpaceRace::Level2CleanUp()
{
	// unregister space ship
	scene.UnRegisterMoving(*spaceShip);

	delete stationTwo->boundingbox;

	// clean fixed game objects
	for (ifix = fixed.begin(); ifix != fixed.end(); ++ifix)
	{
		scene.UnRegisterStatic(*((Sprite *) *ifix));
		delete (*ifix);
	}
	fixed.clear();

	// clean moving game objects
	for (imov = moving.begin(); imov != moving.end(); ++imov)
	{
		scene.UnRegisterMoving(*((Sprite *) *imov));
		delete (*imov)->boundingbox;
		delete (*imov);
	}
	moving.clear();

	// clean missiles
	for (imis = missiles.begin(); imis != missiles.end(); ++imis)
	{
		scene.UnRegisterMoving(*((Geometry *) *imis));
		delete *imis;			
	}
	missiles.clear();

	scene.UnRegisterStatic(finishBB);
}

/**********************************************************************************/